package com.vernhe.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;

/**
 * 初始化器，当Channel注册后，会执行里面的方法来达到初始化的目的
 */
public class HelloServerInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        //向管道中添加Handler

        //1、得到管道
        ChannelPipeline pipeline = socketChannel.pipeline();

        //向管道中添加Handler
        //这里添加的是Netty自带的Handler，作用是对请求和响应做编码解码处理
        pipeline.addLast("HttpServerCodec",new HttpServerCodec());

        //自定义Handler
        pipeline.addLast("customHandler",new CustomHandler());
    }
}
